package com.woniuxy.controller;

import com.woniuxy.entity.Position;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Role;
import com.woniuxy.service.PositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "岗位接口")
@RequestMapping("/position")
@RequiresRoles(value = {"superAdmin","admin"},logical = Logical.OR)
public class PositionController {
    @Autowired(required = false)
    PositionService positionService;

    @GetMapping("/all")
    @ApiOperation(value = "查询所有岗位")
    public ResponseEntity findAll(){
        return positionService.findAll();
    }

    @RequiresPermissions(value = "position:delete")
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除一个岗位")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value="岗位序号",required=true)@PathVariable int id){
        return positionService.deleteOne(id);
    }

    @RequiresPermissions(value = "position:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增一个岗位")
    public ResponseEntity addOne(@ApiParam(name = "Position", value="岗位实体",required=true)@RequestBody Position position){
        return positionService.addOne(position);
    }

    @PostMapping("/search")
    @ApiOperation(value = "查询岗位")
    public ResponseEntity findByContext(@ApiParam(name = "Position", value="岗位实体",required=true)@RequestBody Position position){
        System.out.println(position);
        return positionService.findByContext(position);
    }

    @RequiresPermissions(value = "position:batchDelete")
    @PutMapping("/batchDelete")
    @ApiOperation(value = "批量删除岗位")
    public ResponseEntity batchDelete(@ApiParam(name = "ids", value="岗位id数组",required=true)@RequestBody int[] ids){
        return positionService.batchDelete(ids);
    }

    @GetMapping("/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取分页岗位数据")
    public ResponseEntity findAll(@ApiParam(name = "currentPage", value="当前页码",required=true)@PathVariable int currentPage,
                                  @ApiParam(name = "pageSize", value="页面大小",required=true)@PathVariable int pageSize){
        return positionService.findAll(currentPage, pageSize);
    }

    @RequiresPermissions(value = "position:update")
    @PutMapping("/update")
    @ApiOperation(value = "修改岗位")
    public ResponseEntity updateOne(@ApiParam(name = "position", value="岗位实体",required=true) @RequestBody Position position){
        return positionService.updateOne(position);
    }
}
